/**
 * Created by liubin on 2017/6/30.
 */



var DtjTableDiceLayer = GameCCSBase.extend({
    Image_bg : null,
    Image_dice1 : null,
    Image_dice2 : null,
    Image_bgCap : null,

    Image_diceMove1 : null,
    Image_diceMove2 : null,

    time1 : null,
    time2 : null,
    time3 : null,
    time4 : null,
    ctor: function () {
        this._super();
        this.loadCocostudio("res/ccs/DtjTableDice.json");

        this.Image_bg = mo.seekWidgetByName(this.ccsNode, "Image_bg");
        this.Image_dice1 = mo.seekWidgetByName(this.ccsNode, "Image_dice1");
        this.Image_dice2 = mo.seekWidgetByName(this.ccsNode, "Image_dice2");
        this.Image_bgCap = mo.seekWidgetByName(this.ccsNode, "Image_bgCap");

        this.Image_diceMove1 = mo.seekWidgetByName(this.ccsNode, "Image_diceMove1");
        this.Image_diceMove2 = mo.seekWidgetByName(this.ccsNode, "Image_diceMove2");

        this.time1 = 0.3;
        this.time2 = 0.1;
        this.time3 = 0.3;
        this.time4 = 0.3;
    },

    //骰子动画
    showDiceAnim : function(dices, toViewSeat){
        var maidinglishouTime = 1;
        this.setVisible(false);
        var bgPos = cc.p(mo.gameSize.x / 2, mo.gameSize.y / 2 - 150);

        //bg
        this.Image_bg.stopAllActions();
        this.Image_bg.setPosition(bgPos);
        this.Image_bg.setRotation(0);
        this.Image_bg.setOpacity(255);

        //dice
        this.Image_dice1.stopAllActions();
        this.Image_dice1.setVisible(true);
        this.loadImageFile(this.Image_dice1, "ttz_sezi_" + dices[0] +  "_" + parseInt(Math.random() * 2) + ".png");
        this.Image_dice1.setPosition(cc.p(Math.random() * 16 + 52, Math.random() * 16 + 48));

        this.Image_dice2.stopAllActions();
        this.Image_dice2.setVisible(true);
        this.loadImageFile(this.Image_dice2, "ttz_sezi_" + dices[1] +  "_" + parseInt(Math.random() * 2) + ".png");
        this.Image_dice2.setPosition(cc.p(Math.random() * 16 + 108, Math.random() * 16 + 48));

        //cap
        this.Image_bgCap.stopAllActions();
        this.Image_bgCap.setPosition(cc.p(87.5, 20));
        this.Image_bgCap.setRotation(0);
        this.Image_bgCap.setOpacity(255);

        var that = this;
        this.Image_bg.runAction(cc.sequence(
            cc.delayTime(maidinglishouTime),
            cc.callFunc(function(){
                that.setVisible(true);
            }),
            cc.moveTo(that.time1, cc.p(bgPos.x, bgPos.y + 80)),
            cc.callFunc(function(){
                mo.soundHelper.playDtjRockDice();
            }),
            cc.repeat(cc.sequence(
                cc.rotateTo(that.time2, -25),
                cc.rotateTo(that.time2, 25)
            ), 3),
            cc.callFunc(function(){
                mo.dtjTable.onPlayTableOpAnim();
            }),
            cc.spawn(
                cc.moveTo(that.time3, cc.p(bgPos)),
                cc.rotateTo(that.time3, 0)
            ),
            cc.callFunc(function(){
                that.Image_bgCap.runAction(cc.sequence(
                    cc.spawn(
                        cc.moveTo(that.time4, cc.p(25, 112)),
                        cc.rotateTo(that.time4, -20),
                        cc.fadeOut(that.time4)
                    )
                ));
            }),
            cc.delayTime(0.8),
            cc.fadeOut(0.2)
        ));

        var delayTime = this.time1 + this.time2 * 3 + this.time3 + this.time4 + 1 + maidinglishouTime;

        this.moveDiceAnim(dices, toViewSeat, delayTime);

        return delayTime + 0.5;
    },

    //飞骰子
    moveDiceAnim : function(dices, toViewSeat, delayTime){
        this.Image_diceMove1.stopAllActions();
        this.Image_diceMove1.setVisible(true);
        this.Image_diceMove1.setOpacity(0);
        this.loadImageFile(this.Image_diceMove1, "ttz_sezi_d_" + dices[0] + ".png");
        var pos1 = this.Image_bg.convertToWorldSpace(this.Image_dice1.getPosition());
        this.Image_diceMove1.setPosition(pos1);

        this.Image_diceMove2.stopAllActions();
        this.Image_diceMove2.setVisible(true);
        this.Image_diceMove2.setOpacity(0);
        this.loadImageFile(this.Image_diceMove2, "ttz_sezi_d_" + dices[1] + ".png");
        var pos2 = this.Image_bg.convertToWorldSpace(this.Image_dice2.getPosition());
        this.Image_diceMove2.setPosition(pos2);

        var toPos = mo.dtjCardHelper.getInhandCardPos(toViewSeat);
        var that = this;
        this.Image_diceMove1.runAction(cc.sequence(
            cc.delayTime(delayTime),
            cc.fadeIn(0.1),
            cc.moveTo(0.2, cc.p(toPos.x - 22, toPos.y)),
            cc.fadeOut(0.5)
        ));
        this.Image_diceMove2.runAction(cc.sequence(
            cc.delayTime(delayTime),
            cc.fadeIn(0.1),
            cc.moveTo(0.2, cc.p(toPos.x + 22, toPos.y)),
            cc.fadeOut(0.5),
            cc.callFunc(function(){
                that.setVisible(false);
            })
        ));
    },

    loadImageFile : function(image, file){
        image.ignoreContentAdaptWithSize(false);
        image.ignoreContentAdaptWithSize(true);
        image.loadTexture(file, ccui.Widget.PLIST_TEXTURE);
    }
});